'use strict';

angular.module('App').controller('PatchNewAddController',
    ['$scope', '$stateParams', 'BaseModule', 'BaseFunction', 'AppEnum', '$location', 'Authentication', 'FileUploader',
    function($scope, $stateParams, BaseModule, BaseFunction, AppEnum, $location, Authentication, FileUploader) {
        $scope.DeviceTypeEnum = BaseFunction.getEnum('AppEnum.DeviceType');

        $scope.uploader0 = null;
        $scope.Url = '/api/base/uploadFile';
        $scope.btnSave = function(){
            
            if($scope.Module.App.FocusedRecord.Name == undefined || 
                $scope.Module.App.FocusedRecord.Name.trim() == ""){
                BaseFunction.alert("名称不能为空！", "保存错误");
                return;
            }
            if($scope.Module.App.FocusedRecord.Type == undefined || 
                $scope.Module.App.FocusedRecord.Type == null || 
                $scope.Module.App.FocusedRecord.Type < 0){
                BaseFunction.alert("类型不能为空！", "保存错误");
                return;
            }
            if($scope.Module.App.FocusedRecord.Version == undefined || 
                $scope.Module.App.FocusedRecord.Version.trim() == ""){
                BaseFunction.alert("版本号不能为空！", "保存错误");
                return;
            }
            if(($scope.Module.App.FocusedRecord.FileName == undefined || 
                $scope.Module.App.FocusedRecord.FileName == "") && $scope.uploader0.queue.length <= 0){
                BaseFunction.alert("文件不能为空！", "保存错误");
                return;
            }

            $scope.uploader0.save(function(){
                
                $scope.Module.App.save();                        
                  
            })
        };
        $scope.btnCancel = function(){
            $scope.Module.App.cancel();
        };
        $scope.btnAdd = function(){
            $scope.Module.App.add();
        };
        $scope.btnDelete = function(){
            var filePaths = [];
            if($scope.Module.App.FocusedRecord.DownloadUrl != null && 
                $scope.Module.App.FocusedRecord.DownloadUrl != undefined &&
                $scope.Module.App.FocusedRecord.DownloadUrl != "")
            {
                filePaths.push($scope.Module.App.FocusedRecord.DownloadUrl );
            }
            if($scope.Module.App.FocusedRecord.DisplayImage != null && 
                $scope.Module.App.FocusedRecord.DisplayImage != undefined &&
                $scope.Module.App.FocusedRecord.DisplayImage != "")
            {
                filePaths.push($scope.Module.App.FocusedRecord.DisplayImage );
            }
            if($scope.Module.App.FocusedRecord.MouseDownImage != null && 
                $scope.Module.App.FocusedRecord.MouseDownImage != undefined &&
                $scope.Module.App.FocusedRecord.MouseDownImage != "")
            {
                filePaths.push($scope.Module.App.FocusedRecord.MouseDownImage );
            }
            if(filePaths.length > 0){
                PublicService.deleteFiles(filePaths, function(count){                    
                });
            }
            $scope.Module.App.delete();
        };
        

        //初始化
        BaseModule.initModule($scope, 'App');
        $scope.Module.App.AfterOnAdd = function(record){
            record.IsEffective = true;
            record.Type = AppEnum.DeviceType.Pad.Value;
        };

        if($stateParams.id) {
            $scope.Module.App.findById($stateParams.id, function(record){
                $scope.Module.App.FocusedRecord = record;
            });
        }
        else{
            $scope.btnAdd();
        };
    }
]);